<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class prod extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		$arr = array();
		$pcrow = spClass('m_pcat')->findAll();
		foreach($pcrow as $v){
			$arr[] = array('id'=>$v['id'],'pid'=>$v['pid'],'pcatname'=>$v['pcatname'],'odnum'=>$v['odnum']);
		}
		$treeObj = spClass('Tree',array(array('id','pid','pcatname','format')));
		$this->pcat = $treeObj->getTree($arr);
	}
	//产品列表
	public function lis(){
		$where = ' 1 = 1 ';
		if($search = $this->spArgs('search')){
			if($search['cid'] != ''){
				$where .= " AND cid = {$search[cid]}";
			}
			if($pname = trim($search['pname'])){
				$pname_s = spClass('m_news')->escape('%' . $pname . '%');
				$where .= " AND pname LIKE {$pname_s}";
				$search['pname'] = $pname;
			}
			$this->search = $search;
		}else{
			if($cid = $this->spArgs('cid')){
				$where .= " AND cid = {$cid}";
			}
			if($pname = $this->spArgs('pname')){
				$pname = urldecode($pname);
				$where .= " AND pname LIKE '%{$pname}%'";
			}
			$this->search = array('cid'=>$cid,'pname'=>$pname);
		}
		$page = (int) $this->spArgs('page',1) > 1 ? (int) $this->spArgs('page',1) : 1;
		$this->prod = spClass('m_prod')->spLinker()->spPager($page,15)->findAll($where,'id DESC','id,cid,pname,nosale,remain,salenum,time,views,stick,odnum,visible');
		$this->pager = spClass('m_prod')->spPager()->getPager();
		$this->display('prod_lis.tpl.php');
	}
	//添加产品
	public function add(){
		if(isset($_SESSION["pic_array"]))	unset($_SESSION["pic_array"]);
		$this->ucat = spClass('m_ucat')->findAll(null, 'id ASC');
		$this->display('prod_add.tpl.php');
	}
	//执行添加
	public function doadd(){
		if(false != spClass('m_prod')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('prod','add'));
		$rows = $this->spArgs();
		if(!$rows['visible'])	$rows['visible'] = 0;
		if(!$rows['stick'])	$rows['stick'] = 0;
		$rows['time'] = time();
		$rows['remain'] = max(0, $rows['remain']);
		$rows['minsale'] = max(1, $rows['minsale']);
		$rows['price'] = serialize($rows['price']);
		if(!$insertid = spClass('m_prod')->create($rows))	$this->error('添加产品失败！',spUrl('prod','add'));
 		if(!empty($rows['album'])){
 			foreach($rows['album'] as $v){
 				if(!empty($v)){
 					$thumb = explode('/', $v);
 					$thumb = $thumb[0] . '/' . $thumb[1] . '/' . $thumb[2] . '/thumb_' . $thumb[3];
 					spClass('m_ppic')->create(array('prodid'=>$insertid,'photo'=>$v,'thumb'=>$thumb));
 				}
 			}
 		}
 		$this->success('添加产品成功！',spUrl('prod','lis'));
	}
	//更新产品
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('prod','lis'));
		if(isset($_SESSION["pic_array"]))	unset($_SESSION["pic_array"]);
		if(isset($_SESSION["pic_upd"]))	unset($_SESSION["pic_upd"]);
		$prod = spClass('m_prod')->find(array('id'=>$id));
		$prod['price'] = unserialize($prod['price']);
		foreach($prod['price'] as $k=>$v){
			$v = max(0.00, $v);
			$prod['price'][$k] = sprintf('%.2f', $v);
		}
		$this->prod = $prod;
		$this->ucat = spClass('m_ucat')->findAll(null, 'id ASC');
		$this->ppic = spClass('m_ppic')->findAll(array('prodid'=>$id),'id DESC');
		$this->display('prod_upd.tpl.php');
	}
	//执行更新
	public function doupd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('prod','lis'));
		if(false != spClass('m_prod')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('prod','lis'));
		$rows = $this->spArgs();
		if(!$rows['visible'])	$rows['visible'] = 0;
		if(!$rows['stick'])	$rows['stick'] = 0;
		if(!$rows['nosale']) $rows['nosale'] = 0;
		$rows['minsale'] = max(1, $rows['minsale']);
		$rows['price'] = serialize($rows['price']);
		$rows['remain'] = max(0, $rows['remain']);
		$thumb = spClass('m_prod')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb']) && !empty($rows['thumb']) && $thumb['thumb']!=$rows['thumb']){
			$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			@unlink($thumb['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false == spClass('m_prod')->update(array('id'=>$id),$rows)){
			$this->error('更新产品失败！',spUrl('prod','lis'));
		}
		if(!empty($rows['album'])){
 			foreach($rows['album'] as $v){
 				if(!empty($v)){
 					$thumb = explode('/', $v);
 					$thumb = $thumb[0] . '/' . $thumb[1] . '/' . $thumb[2] . '/thumb_' . $thumb[3];
 					spClass('m_ppic')->create(array('prodid'=>$id,'photo'=>$v,'thumb'=>$thumb));
 				}
 			}
 		}
		if(isset($_GET['page'])){
			$page = max(1, (int) $_GET['page']);
		}else{
			$page = 1;
		}
		$this->success('更新产品成功！',spUrl('prod','lis',array('page'=>$page)));
	}
	//删除产品
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('prod','lis'));
		$ppic = spClass('m_ppic')->findAll(array('prodid'=>$id));
		for($i=0;$i<count($ppic);$i++){
			if(!empty($ppic[$i]['photo'])){
				$photorow = spClass('m_thumb')->find(array('photo'=>$ppic[$i]['photo']));
				if(!empty($photorow['thumb'])) @unlink($photorow['thumb']);
				@unlink($ppic[$i]['photo']);
				spClass('m_thumb')->delete(array('id'=>$photorow['id']));
			}
		}
		spClass('m_ppic')->delete(array('prodid'=>$id));
		$thumb = spClass('m_prod')->find(array('id'=>$id),null,'thumb');
		if(!empty($thumb['thumb'])){
			if(strpos($thumb['thumb'], 'thumb_') !== false){
				$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
			}else{
				$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
			}
			if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
			if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spClass('m_prod')->delete(array('id'=>$id))){
			$this->success('删除产品成功！',spUrl('prod','lis'));
		}else{
			$this->error('删除产品失败！',spUrl('prod','lis'));
		}
	}
	//批量删除
	public function pldel(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要删除的产品编号！',spUrl('prod','lis'));
		for($i=0;$i<count($idrow);$i++){
			$ppic = spClass('m_ppic')->findAll(array('prodid'=>$idrow[$i]));
			for($j=0;$j<count($ppic);$j++){
				if(!empty($ppic[$j]['photo'])){
					$photorow = spClass('m_thumb')->find(array('photo'=>$ppic[$j]['photo']));
					if(!empty($photorow['thumb'])) @unlink($photorow['thumb']);
					@unlink($ppic[$j]['photo']);
					spClass('m_thumb')->delete(array('id'=>$photorow['id']));
				}
			}
			spClass('m_ppic')->delete(array('prodid'=>$idrow[$i]));
			$thumb = spClass('m_prod')->find(array('id'=>$idrow[$i]),null,'thumb');
			if(!empty($thumb['thumb'])){
				if(strpos($thumb['thumb'], 'thumb_') !== false){
					$thumbrow = spClass('m_thumb')->find(array('thumb'=>$thumb['thumb']));
				}else{
					$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['thumb']));
				}
				if(!empty($thumbrow['photo'])) @unlink($thumbrow['photo']);
				if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
				spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
			}
			spClass('m_prod')->delete(array('id'=>$idrow[$i]));
		}
		$this->success('批量删除产品成功！',spUrl('prod','lis'));
	}
	//删除单张图片
	public function delpic(){
		if(!$id = intval($this->spArgs('id')))	exit('0');
		$thumb = spClass('m_ppic')->find(array('id'=>$id),null,'photo');
		if(!empty($thumb['photo'])){
			$thumbrow = spClass('m_thumb')->find(array('photo'=>$thumb['photo']),null,'id,thumb');
			if(!empty($thumbrow['thumb'])) @unlink($thumbrow['thumb']);
			@unlink($thumb['photo']);
			spClass('m_thumb')->delete(array('id'=>$thumbrow['id']));
		}
		if(false != spClass('m_ppic')->delete(array('id'=>$id))) exit('1');
	}
	//更新库存
	public function updrm(){
		$idrow = $this->spArgs('idrow');
		$remain = $this->spArgs('remain');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_prod')->update(array('id'=>$idrow[$i]),array('remain'=>max(0, $remain[$i])));
		}
		if(isset($_GET['page'])){
			$page = max(1, (int) $_GET['page']);
		}else{
			$page = 1;
		}
		if(isset($_GET['cid'])){
			$cid = intval($_GET['cid']);
		}else{
			$cid = '';
		}
		if(isset($_GET['pname'])){
			$pname = trim($_GET['pname']);
		}else{
			$pname = '';
		}
		$this->success('更新库存成功！',spUrl('prod','lis',array('cid'=>$cid,'pname'=>$pname,'page'=>$page)));
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_prod')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		if(isset($_GET['page'])){
			$page = max(1, (int) $_GET['page']);
		}else{
			$page = 1;
		}
		if(isset($_GET['cid'])){
			$cid = intval($_GET['cid']);
		}else{
			$cid = '';
		}
		if(isset($_GET['pname'])){
			$pname = trim($_GET['pname']);
		}else{
			$pname = '';
		}
		$this->success('更新排序成功！',spUrl('prod','lis',array('cid'=>$cid,'pname'=>$pname,'page'=>$page)));
	}
	//更新浏览
	public function updvs(){
		$idrow = $this->spArgs('idrow');
		$views = $this->spArgs('views');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_prod')->update(array('id'=>$idrow[$i]),array('views'=>(int)$views[$i]));
		}
		if(isset($_GET['page'])){
			$page = max(1, (int) $_GET['page']);
		}else{
			$page = 1;
		}
		if(isset($_GET['cid'])){
			$cid = intval($_GET['cid']);
		}else{
			$cid = '';
		}
		if(isset($_GET['pname'])){
			$pname = trim($_GET['pname']);
		}else{
			$pname = '';
		}
		$this->success('更新浏览成功！',spUrl('prod','lis',array('cid'=>$cid,'pname'=>$pname,'page'=>$page)));
	}
	//切换状态|上下架
	public function updns(){
		if(!$idrow = $this->spArgs('id'))	$this->error('请选择要切换状态的编号！',spUrl('prod','lis'));
		for($i=0;$i<count($idrow);$i++){
			$isnosale = spClass('m_prod')->find(array('id'=>$idrow[$i]),null,'nosale');
			if($isnosale['nosale'] == 0){
				$upnosale = 1;
			}else{
				$upnosale = 0;
			}
			spClass('m_prod')->update(array('id'=>$idrow[$i]),array('nosale'=>$upnosale));
		}
		if(isset($_GET['page'])){
			$page = max(1, (int) $_GET['page']);
		}else{
			$page = 1;
		}
		if(isset($_GET['cid'])){
			$cid = intval($_GET['cid']);
		}else{
			$cid = '';
		}
		if(isset($_GET['pname'])){
			$pname = trim($_GET['pname']);
		}else{
			$pname = '';
		}
		$this->success('切换状态成功！',spUrl('prod','lis',array('cid'=>$cid,'pname'=>$pname,'page'=>$page)));
	}
}
